import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { SuccessInterceptor } from './interceptor/success.interceptor'
import { ValidationPipe } from './interceptor/validation.pipe'
import { HttpExceptionFilter } from './interceptor/HttpExceptionFilter'
import { AppModule } from './app.module';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 设置静态资源服务
  app.useStaticAssets(join(__dirname, '..', 'public'), {
    prefix: '/public/',
  }) // http://localhost:3000/public/uploads/xxx/xxx.jpg访问图片

  const config = new DocumentBuilder()
    .setTitle('nest-vue接口')
    .setDescription('nest api 接口')
    .setVersion('1.0')
    // .addTag('接口信息')
    .build();
  const document = SwaggerModule.createDocument(app, config);

  SwaggerModule.setup('api-docs', app, document);

  // 全局注册通用验证管道ValidationPipe
  app.useGlobalPipes(new ValidationPipe());

  // 全局注册通用异常过滤器HttpExceptionFilter
  app.useGlobalFilters(new HttpExceptionFilter());

  // 全局使用请求成功接口输出
  app.useGlobalInterceptors(new SuccessInterceptor());

  // 允许跨域
  // app.enableCors();

  await app.listen(3000);
}
bootstrap();
